<?php
/**
 * Radiant CMS
 * 
 * @package    radiant-cms
 * @copyright  Copyright (c) 7 Group Studios, LLC (http://dev.7gstudios.com)
 * @version	   $Id: init.php 4 2011-07-02 16:36:23Z rpuig@7gstudios.com $
 */

/** External Libraries */
define('RMS_LIB_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR);
/** Application Libraries */
define('RMS_APP_DIR', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR);
/** Bootstrap File */
define('RMS_AUTOLOAD', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'autoload.php');

// update include path
set_include_path(get_include_path() . PATH_SEPARATOR . RMS_APP_DIR . PATH_SEPARATOR . RMS_LIB_DIR);
// register autoload
spl_autoload_register('_lib_autoload');
// error reporting
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
//error_reporting(E_WARNING | E_USER_WARNING | E_ERROR | E_USER_ERROR);
error_reporting(E_ALL);

/**
 * Load file location from a global array to facilitate class auto loading
 * 
 * @ignore
 * @param string $class Name of the class to load
 */
function _lib_autoload( $class )
{
	// normalize class name
	$class = strtolower($class);
	// build autoload file
	if( !is_file(RMS_AUTOLOAD) ) _build_lib_autoload();
	// refresh global path array
	include RMS_AUTOLOAD;
	// check class existense in global path array
	if( isset($__paths[$class]) )
	{
		if( is_file($__paths[$class]) )
		{
			include $__paths[$class];
		}
		else
		{
			// rebuild autoload file and try again
			_build_lib_autoload();
			// refresh global path array
			include RMS_AUTOLOAD;
			// check class existense in global path array once again
			if( isset($__paths[$class]) )
			{
				if( is_file($__paths[$class]) )
				{
					include $__paths[$class];
				}
				else
				{
					throw new Exception('autoload retry failed => invalid class location: ' . $__paths[$class]);
				}
			}
			else
			{
				throw new Exception('autoload retry failed => invalid class name: ' . $class);
			}
		}
	}
	else
	{
		// rebuild autoload file and try again
		_build_lib_autoload();
		// refresh global path array
		include RMS_AUTOLOAD;
		// check class existense in global path array once again
		if( isset($__paths[$class]) )
		{
			if( is_file($__paths[$class]) )
			{
				include $__paths[$class];
			}
			else
			{
				throw new Exception('autoload retry failed => invalid class location: ' . $__paths[$class]);
			}
		}
		else
		{
			throw new Exception('autoload retry failed => invalid class name: ' . $class);
		}
	}
}

/**
 * Create global path array to facilitate class auto loading
 * 
 * @ignore
 */
function _build_lib_autoload()
{
	$__paths = array();
	// search for all php files
	$rdi = new RecursiveDirectoryIterator(RMS_APP_DIR);
	$rii = new RecursiveIteratorIterator($rdi);
	foreach( new RegexIterator($rii, '/^.+\.php$/i') as $file )
	{
		// strip lib dir and .php extension from filename to get class name
		$classPath = substr($file->getPathName(), 0, -4);
		$classTemp = str_replace(RMS_APP_DIR, '', $classPath);
		$className = str_replace(DIRECTORY_SEPARATOR, '_', $classTemp);
		// adding class to map
		$__paths[$className] = $file->getPathName();
	}
	$autoloadDate = date(DATE_ISO8601);
	// create and save autoload file
	$autoloadBody  = "<?php\n";
	$autoloadBody .= "/**\n";
	$autoloadBody .= " * This file is generated automatically, just erase it and it will be regenerated...\n";
	$autoloadBody .= " * \n";
	$autoloadBody .= " * @package radiant-cms\n";
	$autoloadBody .= " * @version {$autoloadDate}\n";
	$autoloadBody .= " */\n\n";
	$autoloadBody .= "\$__paths = array(\n";
	foreach( $__paths as $className => $fileName )
	{
		$autoloadBody .= "  '{$className}' => '{$fileName}',\n";
	}
	$autoloadBody .= ");\n?>\n";
	file_put_contents(RMS_AUTOLOAD, $autoloadBody);
}
